package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.SysDeclaregoods;
import com.ruoyi.system.domain.SysPurchaseplan;
import com.ruoyi.system.domain.SysPurchasingunit;
import com.ruoyi.system.service.ISysDeclaregoodsService;
import com.ruoyi.system.service.ISysPurchaseplanService;
import com.ruoyi.system.service.ISysPurchasingunitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysProcurementdeclare;
import com.ruoyi.system.service.ISysProcurementdeclareService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购申报实体Controller
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/procurementdeclare")
public class SysProcurementdeclareController extends BaseController
{
    private String prefix = "system/procurementdeclare";

    @Autowired
    private ISysProcurementdeclareService sysProcurementdeclareService;
    @Autowired
    private ISysPurchasingunitService sysPurchasingunitService;
    @Autowired
    private ISysPurchaseplanService sysPurchaseplanService;
    @Autowired
    private ISysDeclaregoodsService sysDeclaregoodsService;
    @RequiresPermissions("system:procurementdeclare:view")
    @GetMapping()
    public String procurementdeclare()
    {
        return prefix + "/procurementdeclare";
    }

    /**
     * 查询采购申报实体列表
     */
    @RequiresPermissions("system:procurementdeclare:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysProcurementdeclare sysProcurementdeclare)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        if("10".equals(sysUser.getUserType())){
            sysProcurementdeclare.setPurchasingUnitId(sysUser.getCompanyId());
        }
        startPage();
        List<SysProcurementdeclare> list = sysProcurementdeclareService.selectSysProcurementdeclareList(sysProcurementdeclare);
        return getDataTable(list);
    }

    /**
     * 导出采购申报实体列表
     */
    @RequiresPermissions("system:procurementdeclare:export")
    @Log(title = "采购申报实体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysProcurementdeclare sysProcurementdeclare)
    {
        List<SysProcurementdeclare> list = sysProcurementdeclareService.selectSysProcurementdeclareList(sysProcurementdeclare);
        ExcelUtil<SysProcurementdeclare> util = new ExcelUtil<SysProcurementdeclare>(SysProcurementdeclare.class);
        return util.exportExcel(list, "procurementdeclare");
    }

    /**
     * 新增采购申报实体
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        SysPurchasingunit sysPurchasingunit = sysPurchasingunitService.selectSysPurchasingunitById(sysUser.getCompanyId());

        SysPurchaseplan sysPurchaseplan = new SysPurchaseplan();
        sysPurchaseplan.setPurchasingUnitId(sysUser.getCompanyId());
        sysPurchaseplan.setAttribute3("0");
        List<SysPurchaseplan> sysPurchaseplanList = sysPurchaseplanService.selectSysPurchaseplanList(sysPurchaseplan);
        mmap.put("purchasingUnitId",sysUser.getCompanyId());
        mmap.put("purchasingName",sysUser.getUserName());
        mmap.put("sysPurchasingunit",sysPurchasingunit);
        mmap.put("sysPurchaseplanList",sysPurchaseplanList);
        return prefix + "/add";
    }

    /**
     * 新增保存采购申报实体
     */
    @RequiresPermissions("system:procurementdeclare:add")
    @Log(title = "采购申报实体", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysProcurementdeclare sysProcurementdeclare)
    {
        sysProcurementdeclare.setAttribite3("0");//申报表状态，0未没有申报，1为申报，2为退回，3为重新提交，4为作废
        return toAjax(sysProcurementdeclareService.insertSysProcurementdeclare(sysProcurementdeclare));
    }

    /**
     * 修改采购申报实体
     */
    @GetMapping("/edit/{procurementDeclareId}")
    public String edit(@PathVariable("procurementDeclareId") Long procurementDeclareId, ModelMap mmap)
    {
        SysProcurementdeclare sysProcurementdeclare = sysProcurementdeclareService.selectSysProcurementdeclareById(procurementDeclareId);
        SysPurchaseplan sysPurchaseplan = new SysPurchaseplan();
        sysPurchaseplan.setPurchasingUnitId(sysProcurementdeclare.getPurchasingUnitId());
        sysPurchaseplan.setAttribute3("0");//0为没有申报，1为申报。
        List<SysPurchaseplan> sysPurchaseplanList = sysPurchaseplanService.selectSysPurchaseplanList(sysPurchaseplan);
        mmap.put("sysProcurementdeclare", sysProcurementdeclare);
        mmap.put("sysPurchaseplanList",sysPurchaseplanList);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购申报实体
     */
    @RequiresPermissions("system:procurementdeclare:edit")
    @Log(title = "采购申报实体", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysProcurementdeclare sysProcurementdeclare)
    {
        return toAjax(sysProcurementdeclareService.updateSysProcurementdeclare(sysProcurementdeclare));
    }

    /**
     * 删除采购申报实体
     */
    @RequiresPermissions("system:procurementdeclare:remove")
    @Log(title = "采购申报实体", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String [] ides = ids.split(",");
        int jieguo =0 ;
        for (int i = 0;i <ides.length;i++){
            SysDeclaregoods sysDeclaregoods = new SysDeclaregoods();
            sysDeclaregoods.setProcurementDeclareId(Long.parseLong(ides[i]));
            List<SysDeclaregoods> declaregoods = sysDeclaregoodsService.selectSysDeclaregoodsList(sysDeclaregoods);
            if(declaregoods.size()>0){
                AjaxResult.error("该采购申报有货物商品信息，不允许删除！");
            }else{
              jieguo =  sysProcurementdeclareService.deleteSysProcurementdeclareByIds(ids);
            }
        }
        return toAjax(jieguo);
    }

    /**
     * 添加采购商品
     */
    @RequiresPermissions("system:procurementdeclare:list")
    @GetMapping("/detail")
    public String detail(Long procurementDeclareId,String cgbApproveResult, ModelMap mmap)
    {
        SysProcurementdeclare sysProcurementdeclare = sysProcurementdeclareService.selectSysProcurementdeclareById(procurementDeclareId);
        mmap.put("procurementdeclare", sysProcurementdeclare);
        mmap.put("cgbApproveResult", cgbApproveResult);
        mmap.put("procurementdeclareList", sysProcurementdeclareService.selectSysProcurementdeclareList(sysProcurementdeclare));
        return "system/declaregoods/declaregoods";
    }

    /**
     * 采购审批预览
     * @param procurementDeclareId
     * @param mmap
     * @return
     */
    @GetMapping("/yulan/{procurementDeclareId}")
    public String yulan(@PathVariable("procurementDeclareId")Long procurementDeclareId, ModelMap mmap){
        SysProcurementdeclare sysProcurementdeclare = sysProcurementdeclareService.selectSysProcurementdeclareById(procurementDeclareId);
        sysProcurementdeclare.setCreateTime(sysProcurementdeclare.getCreateTime());
        SysDeclaregoods sysDeclaregoods = new SysDeclaregoods();
        sysDeclaregoods.setProcurementDeclareId(procurementDeclareId);
        List<SysDeclaregoods> sysDeclaregoodsList = sysDeclaregoodsService.selectSysDeclaregoodsList(sysDeclaregoods);
        Double sumPrice = 0.00;
        if(sysDeclaregoodsList.size()>0){
            sysProcurementdeclare.setDeclareGoods(sysDeclaregoodsList);
            for (int i = 0;i<sysDeclaregoodsList.size();i++){
                sumPrice+=sysDeclaregoodsList.get(i).getSumPrice();
            }
        }
        mmap.put("procurementDeclare",sysProcurementdeclare);
        mmap.put("sumPrice",sumPrice);
        return prefix + "/view";
    }




}
